﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Radiation___IM;

namespace RadiationUI
{
    public partial class ChatWindows : Form
    {
        private string m_channel;

        public ChatWindows(string channel)
        {
            InitializeComponent();
            m_channel = channel;
        }

        private void fermerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void send()
        {
            if (textBox1.Text.Length > 0)
            {
                lock (richTextBox1)
                {
                    string to_display = textBox1.Text;

                    if (to_display.Length > 0 && to_display.StartsWith("/"))
                    {
                        string[] splitted_display = to_display.Split(new char[] { ' ' }, 2);

                        if (splitted_display.Length == 2 && splitted_display[1] != null)
                        {
                            StringBuilder sb = new StringBuilder();
                            for (int i = 0, j = splitted_display[1].Length; i < j; i++)
                                sb.Append("*");
                            to_display = splitted_display[0] + " " + sb.ToString();
                        }
                    }

                    string text = " <" + Connection.Nickname + "> : " + to_display + "\r\n";

                    richTextBox1.SelectionStart = richTextBox1.Text.Length;
                    richTextBox1.SelectionColor = Color.Orange;
                    richTextBox1.SelectedText = text;
                    richTextBox1.ScrollToCaret();
                }
                /*byte[] message = UnicodeEncoding.Unicode.GetBytes(AESManager.Instance.Encrypt("[channel=" + m_channel + ";c=0]" + textBox1.Text));
                Connection.Client.Client.BeginSend(message, 0, message.Length, System.Net.Sockets.SocketFlags.None, endSend, this);*/
                Connection.sendAsync("[channel=" + m_channel + ";c=0]" + textBox1.Text);
            }
        }

        /*private void endSend(IAsyncResult result)
        {
            Connection.Client.Client.EndSend(result);
        }*/

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.send();
                this.textBox1.Clear();
                e.Handled = true;
            }
        }

        private void ChatWindows_Load(object sender, EventArgs e)
        {
            this.Text = "Channel : " + m_channel;
            this.startDelayTimer.Start();
        }

        public void addMessage(string from, string message)
        {
            MethodInvoker action = delegate 
            {
                lock (richTextBox1)
                {
                    string text = " <" + from + "> : " + message + "\r\n";

                    richTextBox1.SelectionStart = richTextBox1.Text.Length;

                    if (from != "system")
                        richTextBox1.SelectionColor = Color.Black;
                    else
                        richTextBox1.SelectionColor = Color.Green;

                    richTextBox1.SelectedText = text;
                    richTextBox1.ScrollToCaret();
                }
            };
            richTextBox1.BeginInvoke(action);
        }

        public void addUsers(string[] users)
        {
            MethodInvoker action = delegate
            {
                lock (listBox1)
                {
                    listBox1.Items.Clear();
                    listBox1.Items.AddRange(users);
                }
            };
            listBox1.BeginInvoke(action);
        }

        public void addUsers(string user)
        {
            MethodInvoker action = delegate
            {
                lock (listBox1)
                {
                    if (!listBox1.Items.Contains(user))
                        listBox1.Items.Add(user);
                }
            };
            listBox1.BeginInvoke(action);
        }

        public void removeUsers(string user)
        {
            MethodInvoker action = delegate
            {
                lock (listBox1)
                {
                    if (listBox1.Items.Contains(user))
                        listBox1.Items.Remove(user);
                }
            };
            listBox1.BeginInvoke(action);
        }

        public void renameUsers(string user,string new_user)
        {
            MethodInvoker action = delegate
            {
                lock (listBox1)
                {
                    if (listBox1.Items.Contains(user))
                        listBox1.Items[listBox1.Items.IndexOf(user)] = new_user;
                }
            };
            listBox1.BeginInvoke(action);
        }

        private void ChatWindows_FormClosed(object sender, FormClosedEventArgs e)
        {
            WindowManager.Instance.removeChannel(m_channel);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Connection.sendToAPI(Radiation.Common.Communication.APIMethods.GET_CHANNEL_USERS + " " + m_channel);
        }

        private void startDelayTimer_Tick(object sender, EventArgs e)
        {
            if (this.startDelayTimer.Interval == 100)
            {
                this.startDelayTimer.Stop();
                this.startDelayTimer.Interval = 2000;
                Connection.sendToAPI(Radiation.Common.Communication.APIMethods.GET_CHANNEL_USERS + " " + m_channel);
                this.startDelayTimer.Start();
            }
            else
            {
                if (this.listBox1.Items.Count > 0)
                    this.startDelayTimer.Stop();
                else
                    Connection.sendToAPI(Radiation.Common.Communication.APIMethods.GET_CHANNEL_USERS + " " + m_channel);
            }
        }
    }
}
